const ws = require('../../request/ws')
const config = require('../config/conf')
// const sendWSPush = ws.sendWSPush


function Oumirai(instanceConfig) {
    this.order = instanceConfig.order
    this.group = instanceConfig.group
    this.msg = instanceConfig.msgFormat
    this.qq = instanceConfig.qq

}

Oumirai.prototype = {
    // 发送单消息
    sendSinlgMessage(text) {
        let newText = [{ type: 'Plain', text: text }]
        ws.sendWSPush({
            syncId: '-1',
            command: 'sendGroupMessage',
            content: {
                target: this.group,
                messageChain: newText

            }
        })

    },

    // 发送群发单消息
    sendSinlgsMessage(text) {
        let newText = [{ type: 'Plain', text: text }]
        if (this.group instanceof Array) {
            this.group.forEach(item => {
                ws.sendWSPush({
                    syncId: '-1',
                    command: 'sendGroupMessage',
                    content: {
                        target: item,
                        messageChain: newText
                    }
                })
            })
        }
        else {
            console.log('无法群发群消息')
        }
    },

    // 发送普通文本链式消息
    sendTextMessage(list) {
        return list.map(p => {
            return {
                type: 'Plain',
                text: p
            }
        })
    },

    // at群员发送消息
    sendATGroup(text) {
        return [
            { type: 'At', target: this.qq, display: '' },
            { type: 'Plain', text: text }
        ]
    },

    // 发送图片群消息
    sendGroupImg(imgList) {
        return imgList.map(p => {
            return {
                type: 'Image',
                url: p.image || p
            }
        })
    },

    // 发送base64格式的图片
    sendGroupImgBase(base64) {
        return base64.map(p => {
            return {
                type: 'Image',
                base64: p
            }
        })
    },

    sendGroupMessage(messageChain) {
        ws.sendWSPush({
            syncId: '-1',
            command: 'sendGroupMessage',
            content: {
                target: this.group,
                messageChain: messageChain

            }
        })
    },

    // 获取群列表
    getGroupList() {
        ws.sendWSPush({
            syncId: '-1',
            command: 'groupList',
            content: {}
        }).then(res => {
            console.log('获取群列表')
            console.log(res)
        })
    },

    // 群发群消息
    sendGroupsMessage(messageChain) {
        if (this.group instanceof Array) {
            this.group.forEach(item => {
                ws.sendWSPush({
                    syncId: '-1',
                    command: 'sendGroupMessage',
                    content: {
                        target: item,
                        messageChain: messageChain
                    }
                })
            })
        }
        else {
            console.log('无法群发群消息')
        }
    },

    // 发送合并消息
    sendForwardMessage(msgList) {
        // msgList格式：[
        //    { type: 'Plain', text: '你好' },
        //    { type: 'Image', url: 'link' },
        //    ...
        // ]
        // 格式遵循mirai消息类型格式
        const messageChain = [{
            type: "Forward",
            display: {
                title: "群聊的聊天记录",
                brief: "[聊天记录]",
                source: "聊天记录",
                preview: ["msg1", "msg2", "msg3", "msg4"],
                summary: "查看x条转发消息"
            },
            nodeList: []
        }]
        for (const item of msgList) {
            messageChain[0].nodeList.push({
                senderId: config.qq,
                senderName: config.botName || 'bot',
                messageChain: [item],
            })
        }
        return messageChain
    }
}

module.exports = Oumirai